define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'monitor/dingtalk/index',
                    add_url: 'monitor/dingtalk/add',
                    edit_url: 'monitor/dingtalk/edit',
                    del_url: 'monitor/dingtalk/del',
                    multi_url: 'monitor/dingtalk/multi',
                    table: 'dingtalk_robot',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),},
                        {field: 'robot_name', title: __('Robot_name'),operate: 'LIKE', placeholder: '模糊查询'},
                        {field: 'robot_address', title: __('Robot_address'),operate: 'LIKE', placeholder: '模糊查询',
                        cellStyle: formatTableUnit,
                        formatter: paramsMatter},
                        {field: 'ding_sign', title: __('Sign'),operate: 'LIKE', placeholder: '模糊查询',
                        cellStyle: formatTableUnit,
                        formatter: paramsMatter},
                        {field: 'remark', title: __('Remark'),operate: 'LIKE', placeholder: '模糊查询'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });


            function paramsMatter(value, row, index, field) {
                var span = document.createElement('span');
                span.setAttribute('title', value);
                span.innerHTML = value;
                return span.outerHTML;
            }

            function formatTableUnit(value, row, index) {
                return {
                    css: {
                        "white-space": "nowrap",
                        "text-overflow": "ellipsis",
                        "overflow": "hidden",
                        "max-width": "400px"
                    }
                }
            }
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});